package de.wittekind.bjs.frontend;

import java.io.IOException;
import java.text.MessageFormat;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import de.wittekind.bundesjugendspiele.server.interfaces.Discipline;
import de.wittekind.bundesjugendspiele.server.interfaces.Try;
import de.wittekind.bundesjugendspiele.server.interfaces.Unit;
/**
 * @author Fynn Hauptmeier
 */
public class Entry {
	private Try corTry;
	private String text;
	private boolean active;
	private boolean focus;
	private static Logger logger = LogManager.getLogger(Entry.class.getName());
	private Double min;//in Seconds
	private Double sec;

	public Entry(Try tr, boolean active) {
		corTry = tr;
		this.active = active;
	}

	public Entry(Try tr, boolean active, boolean focus) {
		this(tr, active);
		this.focus = focus;
	}

	public String getText() {
		return text;
	}

	public Unit getUnit() {
		try {
			return corTry.getUnit();
		} catch (IOException e) {
			logger.error("Error", e);
		}
		return null;
	}

	public boolean getReadonly() {
		if (!active)
			return true;
		return false;
	}

	public Double getValue() {

		try {
			if (Discipline.AUSDAUER.hasDiscipline(corTry.getDiscipline()))
				return -1D;
			double val = corTry.getValue();
			if (val > 0)
				return val;

		} catch (IOException e) {
			logger.error("Error", e);
		}

		return null;
	}

	public Double getValueMin() {
		double val = -1;
		try {
			val = corTry.getValue();

		} catch (IOException e) {
			logger.error("Error", e);
		}
		val = new Double(val).intValue()/60;
		if (val > 0)
			return val;
		return null;
	}

	public Double getValueSec() {
		double val = -1;
		try {
			val = corTry.getValue();

		} catch (IOException e) {
			logger.error("Error", e);
		}
		val = val % 60;
		if (val > 0)
			return val;
		return null;
	}
	
	public void setValueMin(Double min)
	{
		this.min=min*60;
		if(sec!=null)
		{
			double val=this.min+sec;
			setValue(val);
		}
	}
	public void setValueSec(Double sec)
	{
		this.sec=sec;
		if(min!=null)
		{
			double val=this.min+sec;
			setValue(val);
		}
	}

	
	public void setValue(Double valueAfter) {

		Double valueBefore = getValue();

		if (valueBefore != valueAfter && valueAfter != null
				&& valueAfter.doubleValue() != 0.0) {
			try {
				corTry.setValue(valueAfter);
			} catch (IOException e) {
				logger.error("Error", e);
			}
			if (valueBefore == null) {
				valueBefore = new Double(0);
			}
			log(valueBefore.toString() + getUnit(), valueAfter.toString()
					+ getUnit());
		}

	}

	public void log(String before, String after) {
		try {
			FacesContext context = FacesContext.getCurrentInstance();
			RiegenBean rB = (RiegenBean) FacesContext.getCurrentInstance()
					.getExternalContext().getSessionMap().get("riegenBean");
			HttpServletRequest httpServletRequest = (HttpServletRequest) context
					.getExternalContext().getRequest();

			String ip = httpServletRequest.getRemoteAddr();
			String id = new Integer(rB.getChoosedSchuelerID()).toString();
			String name = rB.getChoosedSchueler().getFirstName() + " "
					+ rB.getChoosedSchueler().getLastName();
			Object[] messages = { name, id, ip, before, after };

			String propertiesText = context.getApplication()
					.evaluateExpressionGet(context, "#{msg['valueChanged']}",
							String.class);
			String message = MessageFormat.format(propertiesText, messages);
			logger.info(message);

		} catch (IOException e) {
			logger.error("Error", e);
		}
	}

	public String getFocus() {
		if (focus)
			return "focus";
		return "";
	}
}
